<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>upload模块快速使用</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css">
    <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">


        <div class="layui-col-md12">


            <fieldset class="layui-elem-field">
                <legend> PHP插件系统第二季 v0.2版</legend>
                <div class="layui-field-box">
                    提供了与layui上传模块的对接 以下是各种形式的对接演示
                </div>
            </fieldset>


        </div>
        <div class="layui-col-md12">


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>高级应用：多个插件同时导入</legend>
            </fieldset>

            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="testList">选择插件</button>
                <div class="layui-upload-list">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>文件名</th>
                            <th>大小</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="demoList"></tbody>
                    </table>
                </div>
                <button type="button" class="layui-btn" id="testListAction">开始上传插件</button>
            </div>


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>选完文件后不自动上传</legend>
            </fieldset>

            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
                <button type="button" class="layui-btn" id="test9">开始上传</button>
            </div>


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>拖拽上传 带进度条</legend>
            </fieldset>


            <div class="layui-upload-drag" id="test10">
                <i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>

            </div>


            <div class="layui-hide" id="uploadProgress">
                <br>
                <div class="layui-progress" lay-filter="demo" lay-showPercent="yes">
                    <div class="layui-progress-bar layui-bg-red" lay-percent="1%"></div>
                </div>
                <br>
            </div>

            <div class="layui-hide" id="uploadDemoView">

                <hr>
                <a href="#" target="_blank">点击访问您生成的插件</a>
            </div>
            <hr>


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>单个按钮形式</legend>
            </fieldset>

            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传插件
            </button>


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>任意文字形式</legend>
            </fieldset>


            <div id="checkMe">点我上传</div>

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>原始无对接的状态</legend>
            </fieldset>

            <form action="/index/index/upload" enctype="multipart/form-data" method="post">
                <input type="file" name="image"/> <br>
                <input type="submit" value="上传"/>
            </form>
        </div>

    </div>
</div>

<script>
    layui.use(['upload', 'element', 'jquery'], function () {
        var upload = layui.upload;
        var element = layui.element;
        var $ = layui.jquery;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: '/index/index/layuiUploadAPI' //上传接口
            , field: 'pluginFile' //对接的name字段名
            , accept: 'file' //文件类型
            , exts: 'zip' //文件后缀
            , acceptMime: 'application/x-zip-compressed' //打开文件选择框时，筛选出的文件类型
            , size: 1024 // 文件大小
            , done: function (res) {
                //上传完毕回调
                console.log(res)
                console.log('ok')
                layer.msg(res.info)
            }
            , error: function () {
                //请求异常回调
                console.log('no')
            }
        });


        //拖拽上传
        upload.render({
            elem: '#test10'
            , url: '/index/index/layuiUploadAPI' //上传接口
            , field: 'pluginFile' //对接的name字段名
            , accept: 'file' //文件类型
            , exts: 'zip' //文件后缀
            , acceptMime: 'application/x-zip-compressed' //打开文件选择框时，筛选出的文件类型
            , size: 10240 // 文件大小
            , auto: true
            //,multiple: true
            , choose: function () {
                layui.$('#uploadProgress').removeClass('layui-hide');
            }
            , progress: function (n) {
                var percent = n + '%' //获取进度百分比
                element.progress('demo', percent); //可配合 layui 进度条元素使用
                console.log(percent); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
            }
            , done: function (res) {
                layer.msg(res.info);
                if (res.status == 1) {
                    layui.$('#uploadDemoView').removeClass('layui-hide').find('a').attr('href', './' + res.url);
                }

                console.log(res)
            }
        });

        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            , url: '/index/index/layuiUploadAPI' //上传接口
            , field: 'pluginFile' //对接的name字段名
            , accept: 'file' //文件类型
            , auto: false
            //,multiple: true
            , bindAction: '#test9'
            , done: function (res) {
                layer.msg(res.info);
                console.log(res)
            }
        });

        //任意位置调用上传
        upload.render({
            elem: '#checkMe'
            , url: '/index/index/layuiUploadAPI' //上传接口
            , field: 'pluginFile' //对接的name字段名
            , accept: 'file' //文件类型
            , auto: true
            , done: function (res) {
                layer.msg(res.info);
                console.log(res)
            }
        });


        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: '/index/index/layuiUploadAPI' //上传接口
            , field: 'pluginFile' //对接的name字段名
            , accept: 'file'
            , multiple: true
            , acceptMime: 'application/x-zip-compressed' //打开文件选择框时，筛选出的文件类型
            , auto: false
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            , progress: function (n, index) {
                var percent = n + '%' //获取进度百分比

                console.log(percent); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
                // console.log(index); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。

                // var tr = demoListView.find('tr#upload-' + index)
                //     , tds = tr.children();
                // tds.eq(4).html('<span style="color: #ff5722;">上传失败 ' + percent + '% </span>');

            }
            , done: function (res, index, upload) {


                if (res.status) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(res, index, upload);
            }
            , error: function (res, index, upload) {


                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #ff5722;">上传失败 ' + res.info + '</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
    });
</script>
</body>
</html>